package com.qj.yantaishan.service.impl;

import java.util.*;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.common.utils.DateUtils;
import com.qj.yantaishan.common.utils.security.Md5Utils;
import com.qj.yantaishan.common.utils.uuid.UUID;
import com.qj.yantaishan.domain.AuthLogin;
import com.qj.yantaishan.domain.AutoLogin;
import com.qj.yantaishan.domain.Permission;
import com.qj.yantaishan.mapper.AuthLoginMapper;
import com.qj.yantaishan.mapper.UserVoMapper;
import com.qj.yantaishan.service.IPermissionService;
import com.qj.yantaishan.service.IUserVoService;
import com.qj.yantaishan.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;

import org.springframework.stereotype.Service;
import com.qj.yantaishan.mapper.AdminMapper;
import com.qj.yantaishan.domain.Admin;
import com.qj.yantaishan.service.IAdminService;
import com.qj.yantaishan.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author qihe
 * @date 2022-07-22
 */
@Service
@Primary

public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AuthLoginMapper authLoginMapper;
    @Autowired
    private IUserVoService userVoService;
    @Autowired
    private UserVoMapper userVoMapper;
    @Autowired
    private IPermissionService permissionService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Admin selectAdminById(Long id) {
        return adminMapper.selectAdminById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param admin 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Admin> selectAdminList(Admin admin) {
        return adminMapper.selectAdminList(admin);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param admin 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAdmin(Admin admin) {
        return adminMapper.insertAdmin(admin);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param admin 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAdmin(Admin admin) {

        return adminMapper.updateAdmin(admin);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdminByIds(String ids) {
        return adminMapper.deleteAdminByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdminById(Long id) {
        return adminMapper.deleteAdminById(id);
    }

    @Override
    public Map<String, Object> login(String account, String pwd) {
        Map<String, Object> result = new HashMap<>();
        Admin admin = adminMapper.selectOne(new LambdaQueryWrapper<Admin>().eq(Admin::getAccount, account));
        UserVo userVo = userVoMapper.selectOne(new LambdaQueryWrapper<UserVo>().eq(UserVo::getPhone, account));
        int flag = 0;
        if (userVo == null) {
            result.put("token", "0");
            return result;
        }
        flag = authLoginMapper.selectManageByUid(userVo.getUid());

        if (flag == 0) {
            result.put("token", "0");
            return result;
        }
        String hashPwd = Md5Utils.hash(pwd);
        if (!hashPwd.toUpperCase().equals(userVo.getPassword())) {
            result.put("token", "0");
        } else {
//            密码正确
            StpUtil.login(userVo.getUid());
            result.put("token", StpUtil.getTokenValue());
        }
//        UUID uuid = UUID.randomUUID();
//        AutoLogin autoLogin = authLoginMapper.selectOne(new LambdaQueryWrapper<AutoLogin>().eq(AutoLogin::getUserId, admin.getId()));
//        if (autoLogin == null) {
//            autoLogin = new AutoLogin();
//            autoLogin.setToken(uuid.toString());
//            autoLogin.setUserId(admin.getId());
//            authLoginMapper.insert(autoLogin);
//        }else{
//            autoLogin.setToken(uuid.toString());
//            authLoginMapper.updateById(autoLogin);
//        }


        result.put("adminInfo", userVo);
        return result;
    }

    @Override
    public void logout(String token) {
        authLoginMapper.delete(new LambdaQueryWrapper<AutoLogin>().eq(AutoLogin::getToken, token));
    }

    @Override
    public Map<String, Object> selectManageList(Integer currentPage, Integer pageSize) {
        Map<String, Object> result = new HashMap<>();
        int current = (currentPage - 1) * pageSize;
        List<Map<String, Object>> records = adminMapper.selectManageList(current, pageSize);
        int total = adminMapper.total();
        int pages = ((total + pageSize) - 1) / pageSize;
        result.put("current", currentPage);
        result.put("size", pageSize);
        result.put("total", total);
        result.put("pages", pages);
        result.put("records", records);

        return result;
    }

    /**
     * 添加管理员
     *
     * @param uid
     * @return
     */
    @Override
    @Transactional
    public int addStoreManage(Integer uid) {


        //1.先查询 该用户是否存在
        UserVo userVo = userVoMapper.selectById(uid);
        //不存在返回404
        if (userVo == null) return 404;
        //存在就添加
        if (adminMapper.selectManage(uid) != 0) return 501;
        long addTime = new Date().getTime() / 1000;
        adminMapper.addStoreManage(uid, addTime);
        //添加权限  有多少加多少
        List<Permission> permissions = permissionService.list();

        for (Permission p : permissions) {
            permissionService.addManagePermssion(p.getId(), uid);
        }


        return 0;

    }

    @Override
    public void deleteManage(Integer uid) {
        permissionService.deleteManagePermission(uid);
        adminMapper.deleteManage(uid);
    }
}
